package com.dh.leetcode.four;

import org.junit.Assert;
import org.junit.Test;

/**
 * https://leetcode-cn.com/problems/island-perimeter/
 *
 * 463. 岛屿的周长
 */
public class _463_landPerimeter {

    public int islandPerimeter(int[][] grid) {
        int zc = 0;
        int yl = grid.length;
        int xl;
        for (int i = 0; i < yl; i++) {
            xl = grid[i].length;
            for (int j = 0; j < xl; j++) {
                int a = grid[i][j];
                if (a == 1) {
                    //左
                    if (j == 0) {
                        zc++;
                    } else if (grid[i][j - 1] == 0) {
                        zc++;
                    }

                    //上
                    if (i == 0) {
                        zc++;
                    } else if (grid[i - 1][j] == 0) {
                        zc++;
                    }

                    //右
                    if (j == xl - 1) {
                        zc++;
                    } else if (grid[i][j + 1] == 0) {
                        zc++;
                    }

                    //下
                    if (i == yl - 1) {
                        zc++;
                    } else if (grid[i + 1][j] == 0) {
                        zc++;
                    }
                }
            }
        }
        return zc;
    }

    @Test
    public void testislandPerimeter() {
        int[][] grid = new int[][]{{0, 1, 0, 0}, {1, 1, 1, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}};
        Assert.assertEquals(islandPerimeter(grid), 16);


        int[][] grid2 = new int[][]{{0, 1}};
        Assert.assertEquals(islandPerimeter(grid2), 4);
    }
}
